<template>
  <a-modal
    :title="'选择价格方案'"
    :visible="true"
    width="70%"
    :footer="null"
    @cancel="reset"
  >
    <div class="main">
      <a-card>
        <a-table
          bordered
          :scroll="{x: '100%'}"
          :pagination="false"
          :row-selection="{ selectedRowKeys: selectedRowKeys,selectedRows:selectedRows, onChange: onSelectChange,type:'radio' }"
          :columns="columns"
          :loading="loading"
          :data-source="supplyList"
        />

      </a-card>
    </div>

  </a-modal>
</template>

<script>
import { supplyPriceList } from '@api/material/index'

const columns = [
  {
    title: '供应商编号',
    width: 120,
    dataIndex: 'supplyCode'
  },
  {
    title: '方案名称',
    width: 100,
    dataIndex: 'name'
  },
  {
    title: '0CM基价',
    width: 100,
    dataIndex: 'basePrice'
  },
  {
    title: '每CM单价',
    width: 100,
    dataIndex: 'unitPrice'
  },
  {
    title: '基准单位长度',
    width: 140,
    dataIndex: 'minQty'
  },
  {
    title: '最小单价',
    width: 100,
    dataIndex: 'minPrice'
  },
  {
    title: '小于基长每CM单价',
    width: 170,
    dataIndex: 'lunitPrice'
  },
  {
    title: '小于基长特殊价格',
    width: 170,
    dataIndex: 'lotherPrice'
  },
  {
    title: '小于基长基价',
    width: 150,
    dataIndex: 'lbasePrice'
  },
  {
    title: '特殊价格',
    width: 100,
    dataIndex: 'other'
  },
  {
    title: '折扣',
    width: 80,
    dataIndex: 'discount'
  },
  {
    title: '税率',
    width: 80,
    dataIndex: 'cess'
  },
  {
    title: '备注',
    width: 200,
    dataIndex: 'remark'
  }
]
export default {
  components: {},

  props: {
    supplyCode: {
      type: String,
      default: null
    }
  },

  data() {
    return {
      urlKey: 'basemgt-material/',
      labelCol: { span: 2 },
      wrapperCol: { span: 20 },
      supplyList: [],
      columns,
      loading: false,
      brand: [],
      accountInfo: {},
      form: this.$form.createForm(this),
      dataList: [],
      selectedRowKeys: [],
      selectedRows: [],
      password: '',
      groupId: '',
      roleList: [],
      roleIdList: [],
      showTable: false
    }
  },

  created() {
    const { supplyCode } = this.$props
    if (supplyCode) {
      this.loadData(supplyCode)
    }
  },
  mounted() {},
  computed: {
    jobNumber() {
      return this.$store.getters.userInfo.workNo || ''
    }
  },
  methods: {
    reset() {
      this.$emit('closePrice')
    },
    async loadData(supplyCode) {
          this.loading = true

      try {
        const res = await supplyPriceList({ supplyCode: supplyCode })
        if (res.code == 200) {
          this.supplyList = res.data
           this.loading = false
        } else {
          if (res.msg) {
            this.$message.error(res.msg)
          } else {
            this.$message.error('获取价格方案列表失败！')
          }
           this.loading = false
        }
      } catch (error) {
        this.$message.error('获取价格方案列表失败！')
        this.loading = false
      }
    },
    onSelectChange(selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
      this.$emit('closePrice',this.selectedRows[0])
    },
    getDetail() {
      const params = {
        accountId: this.accountId
      }
      try {
        postAction(`${this.urlKey}account/info`, params).then(res => {
          if (res.code == 200) {
            this.accountInfo = res.data
            res.data.roleList.map(item => {
              this.roleIdList.push(item.roleId)
            })
          } else {
            this.$message.warning(res.msg)
          }
        })
      } catch (error) {
        this.$message.warning('获取详情失败！')
      }
    },
    getRoleList() {
      const data = {}
      try {
        postAction(`${this.urlKey}role/full`, data).then(data => {
          if (data.code == 200) {
            this.roleList = data.data
          } else {
            this.$message.warning(data.msg)
          }
          this.loading = false
        })
      } catch (error) {
        this.loading = false
      }
    },
    getQueryParams() {
      const params = this.form.getFieldsValue()
      return {
        ...params
      }
    },

    submit() {
      if (this.showTable && this.selectedRows.length == 0) {
        this.$message.warning('请选择账号的用户！')
      } else {
        const params = {
          name: this.showTable ? this.selectedRows[0].name : this.accountInfo.name,
          account: this.showTable ? this.selectedRows[0].email : this.accountInfo.account,
          jobNumber: this.showTable ? this.selectedRows[0].jobNumber : this.accountInfo.jobNumber,
          password: this.showTable ? this.selectedRows[0].jobNumber : this.accountInfo.jobNumber,
          mobile: this.showTable ? this.selectedRows[0].mobile : this.accountInfo.mobile,
          roleIdList: this.roleIdList
        }
        if (!this.showTable) {
          params.accountId = this.accountId
        }
        const FNCName = this.showTable ? `${this.urlKey}account/save` : `${this.urlKey}account/update`
        try {
          postAction(FNCName, params).then(res => {
            if (res.code == 200) {
              this.$message.success(this.accountId ? '编辑成功！' : '新增成功！')
              this.reset()
            } else {
              this.$message.warning(res.msg)
            }
          })
        } catch (error) {
          this.$message.warning('操作失败！')
        } finally {
          this.loading = false
        }
      }
    }
  }
}
</script>
<style scoped>
.info-action {
  display: flex;
  flex-direction: row;
  flex-wrap: nowrap;
  align-items: center;
  justify-content: flex-end;
}
.ml-20 {
  margin-left: 20px;
}
</style>